import { getStorage } from "./util";
/**
 * @todo Promise风格AXIOS封装
 * @param {RequestInterface} Object  
 */
export const axios = async (Object: RequestInterface): Promise<any> => {
  const token = await getStorage("token");
  // 服务器地址
  // const BASEURL = "https://www.lv666.top:3384";
  // 本地局域网
  const BASEURL = "http://192.168.0.101:3000";
  return new Promise((resovle, reject) => {
    wx.request({
      url: BASEURL + Object.url,
      method: Object.method,
      data: Object.data,
      timeout: Object.timeout ? Object.timeout : 10000,
      header: {
        token
      },
      success(res: any) {
        if (res.header.token) {
          wx.setStorage({ key: "token", data: res.header.token });
        }
        if (res.data.flag) {
          resovle(res.data)
        } else {
          reject(res.data)
        }
      },
      fail(err: any) {
        reject(err)
      },
    })
  })

}